//
//  PersonalCenterVC.m
//  APP_login
//
//  Created by edz on 7/12/21.
//  Copyright © 2021 baicai. All rights reserved.
//
#import "BZKTools.h"
#import "PersonalCenterVC.h"
#import "MyAttentionVC.h"
#import "Masonry.h"


@interface PersonalCenterVC () <UIScrollViewDelegate>
@property (nonatomic, strong) UIImageView *smallImageView;
@property (nonatomic, strong) UIImageView *bigImageView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, assign) CGRect bigOrginFrame;
@property (nonatomic, assign) BOOL isEnlarge;
@property (nonatomic, strong) UIView *redView;
@property (nonatomic, assign) CGRect smallOrginFrame;
@property (nonatomic, assign) CGPoint smallCenter;
@end

@implementation PersonalCenterVC

- (void) viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"个人中心";
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightButtonClick)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    _redView = [[UIView alloc] init];
    [self.view addSubview: _redView];
    [_redView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.left.mas_equalTo(100);
        make.width.height.mas_equalTo(100);
    }];
    _redView.backgroundColor = [UIColor redColor];
    
    _smallImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon"]];
    [_redView addSubview:_smallImageView];
//    _smallImageView.frame = CGRectMake(10, 10, 80, 80);
    [_smallImageView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.left.mas_equalTo(10);
        make.width.height.mas_equalTo(80);
    }];
    self.navigationController.navigationBar.translucent = NO;
    
        
    _smallImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *imageTag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)];
    [_smallImageView addGestureRecognizer:imageTag];
}

#pragma mark - 大图显示动画
- (void) imageClick:(UIImageView*) imgView{
    
    [[self getKeyWindow] addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
    }];
    
    _smallOrginFrame = [_redView convertRect:_smallImageView.frame toView:self.view];
    self.bigImageView.frame = _smallOrginFrame;
    //大图动画
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.scrollView addSubview:self.bigImageView];
        self.bigImageView.frame = self.bigOrginFrame;
        self.scrollView.backgroundColor = [UIColor colorWithWhite:0 alpha:1];
    } completion:^(BOOL finish){}];
    
}

#pragma mark - 缩小动画
- (void) bigImageClick{
    if (_isEnlarge) {
        _bigImageView.frame = _bigOrginFrame;
        _isEnlarge = NO;
        return;
    }
    [self scrollViewClick];
}

- (void) scrollViewClick{
    self.smallCenter = self.smallImageView.center;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.bigImageView.frame = CGRectMake(110, 150, 80, 80);
        self.scrollView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    } completion:^(BOOL finish){
        [self.bigImageView removeFromSuperview];
        [self.scrollView removeFromSuperview];
        self.scrollView = nil;
        self.bigImageView = nil;
    }];
}

#pragma mark -拖动事件
- (void) scrollViewPan:(UIPanGestureRecognizer*) pan{
    CGPoint point = [pan translationInView:_scrollView];
    if (pan.numberOfTouches == 1) {
        CGPoint newCenter = CGPointMake(self.bigImageView.center.x+point.x, self.bigImageView.center.y+point.y);
        newCenter.y = MIN((SCREEN_HEIGHT+SCREEN_WIDTH-40)/2, newCenter.y);
        newCenter.x = MIN(SCREEN_WIDTH-40, newCenter.x);
        newCenter.x = MAX(20.0f, newCenter.x);
        self.bigImageView.center = newCenter;
        if (newCenter.y > 409) {
            CGFloat y = (newCenter.y-400)/1000;
            self.bigImageView.transform = CGAffineTransformMakeScale(1-y, 1-y);
            self.scrollView.backgroundColor = [UIColor colorWithWhite:0 alpha:1-y];
        }else{
           self.bigImageView.transform = CGAffineTransformIdentity;
           self.scrollView.backgroundColor = [UIColor colorWithWhite:0 alpha:1];
        }
    }
    if (pan.state == UIGestureRecognizerStateEnded) {
        if (self.bigImageView.center.y > 630) {
            [self scrollViewClick];
        }else{
            [self scrollViewPanEnd];
        }
    }
    [pan setTranslation:CGPointZero inView:self.scrollView];
}

- (void) scrollViewPanEnd{
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        //对矩阵还原
        self.bigImageView.transform = CGAffineTransformIdentity;
        self.bigImageView.frame = self.bigOrginFrame;
        self.scrollView.backgroundColor = [UIColor colorWithWhite:0 alpha:1];
       } completion:^(BOOL finish){}];
}

- (void) bigImageViewPinch:(UIPinchGestureRecognizer*) pinch{
    UIView *view = (UIView*)pinch.view;
    view.transform = CGAffineTransformScale(view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;
    if (_bigOrginFrame.size.width == _bigImageView.frame.size.width) {
        _isEnlarge = NO;
    }else{
        _isEnlarge = YES;
    }
}

- (void) rightButtonClick{
    MyAttentionVC *myAttention = [[MyAttentionVC alloc] init];
    [self.navigationController pushViewController:myAttention animated:YES];
}

#pragma mark - 初始化背景图 设置大小 手势
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        _scrollView.scrollEnabled = YES;
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
        _scrollView.bounces = YES;
        _scrollView.alwaysBounceVertical = YES;
        _scrollView.alwaysBounceHorizontal = YES;
        _scrollView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *scrollViewTag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewClick)];
        [_scrollView addGestureRecognizer:scrollViewTag];
        
        UIPanGestureRecognizer *scrollViewPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewPan:)];
        [_scrollView addGestureRecognizer:scrollViewPan];
    }
    return _scrollView;
}
#pragma mark - 初始化大图 设置大小 图片
- (UIImageView *)bigImageView {
    if (_bigImageView == nil) {
       _bigImageView = [[UIImageView alloc] init];
       _bigOrginFrame = CGRectMake(20, (SCREEN_HEIGHT - SCREEN_WIDTH-40)/2, SCREEN_WIDTH-40, SCREEN_WIDTH-40);
       [_bigImageView setImage:_smallImageView.image];
       _bigImageView.userInteractionEnabled = YES;
       
       UITapGestureRecognizer *bigImageTag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bigImageClick)];
       [_bigImageView addGestureRecognizer:bigImageTag];
       
       UIPinchGestureRecognizer *bigImageViewPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(bigImageViewPinch:)];
       [_bigImageView addGestureRecognizer:bigImageViewPinch];
    }
    return _bigImageView;
}

- (UIWindow*)getKeyWindow{
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        if (window.isKeyWindow) {
            return window;
        }
    }
    return nil;
}
/*


// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
